﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.Auditing;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Web.Models;
using FwProject.Authorization;
using FwProject.Authorization.Roles;
using FwProject.Controllers;
using FwProject.Roles.Dto;
using FwProject.ShopApplication;
using FwProject.Web.Models;
using Microsoft.AspNetCore.Mvc;
using X.PagedList;

namespace FwProject.Web.Controllers
{
    //[AbpMvcAuthorize(PermissionNames.Pages_Roles)]
    public class RolesController: FwProjectControllerBase
    {
        private readonly IRepository<Role, int> _repositoryRole;
        private readonly RoleManager _roleManager;
        private readonly IPermissionManager _permissionManager;

        public RolesController(IRepository<Role, int> repositoryRole, RoleManager roleManager, IPermissionManager permissionManager)
        {
            _repositoryRole = repositoryRole;
            _roleManager = roleManager;
            _permissionManager = permissionManager;
        }

        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]
        public async Task<IActionResult> GetRolesData(int page,int limit)
        {
            var model = await _repositoryRole.GetAll().ToPagedListAsync(page,limit);
            LayerDataBaseDto dto = new LayerDataBaseDto();
             
            dto.Data = model;
            dto.Count = model.TotalItemCount;
            return Json(dto);
        }
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <returns></returns>
        public IActionResult Create()
        {
            return View();
        }

        public async Task<IActionResult> CreateRole(RoleInput input)
        {
            var model = input.MapTo<Role>();
            var data = await _roleManager.CreateAsync(model);
            CheckErrors(data);
            return Json(1);
        }

        public async Task<IActionResult> Edit(int id)
        {
            ViewBag.Id = id;
            var model = await _repositoryRole.GetAsync(id);
            
            return View(model.MapTo<RoleInput>());
        }

        public async Task<IActionResult> EditRole(int id, RoleInput input)
        {
            var model = await _repositoryRole.GetAsync(id);
            input.MapTo(model);
            var check = await _roleManager.CheckDuplicateRoleNameAsync(id, input.Name, input.DisplayName);
            if (check.Succeeded)
            {
                //Microsoft.AspNet.Identity 封装的操作
                var identityResult = await _roleManager.UpdateAsync(model);

                CheckErrors(identityResult);
            }
            return Json(id);
        }

        public async Task<IActionResult> Delete(int id)
        {
            var model = await _repositoryRole.GetAsync(id);

            await _roleManager.DeleteAsync(model);
            return Json(id);
        }

        public async Task<IActionResult> Permission(int id)
        {
            ViewBag.Id = id;
            var allPermissions = PermissionManager.GetAllPermissions();
            var currentRolePermissions = await _roleManager.GetGrantedPermissionsAsync(id);
            var model = new RoleGrantViewModel(allPermissions, currentRolePermissions);
            return View(model);
        }

        public async Task<IActionResult> UpdatePermissions(int id, RolePermissionsUpdateDto input)
        {
            var role = await _roleManager.GetRoleByIdAsync(id);
            var grantedPermissions = _permissionManager.GetAllPermissions().Where(p => input.GrantedPermissionNames.Contains(p.Name)).ToList();

            await _roleManager.SetGrantedPermissionsAsync(role, grantedPermissions);
            return Json("");
        }
    }
}
